#include "ti_msp_dl_config.h"
#include "Variables.h"
// #include <cstdint>
//------------------------------------------编码器外部中断begin---------------------------------------------//
uint32_t Exit_Sta_Left;
uint32_t Exit_Sta_Right;

int32_t Encoder_Count_Left;
int32_t Encoder_Count_Right;
int32_t Encoder_Count_Left_temp;
int32_t Encoder_Count_Right_temp;

void GROUP1_IRQHandler(void)
{
    Exit_Sta_Left = DL_GPIO_getEnabledInterruptStatus(GPIO_Encoder_Left_PORT,GPIO_Encoder_Left_PIN_AL_PIN | GPIO_Encoder_Left_PIN_BL_PIN);
    Exit_Sta_Right = DL_GPIO_getEnabledInterruptStatus(GPIO_Encoder_Right_PORT,GPIO_Encoder_Right_PIN_AR_PIN | GPIO_Encoder_Right_PIN_BR_PIN);

    if((Exit_Sta_Left & GPIO_Encoder_Left_PIN_AL_PIN) == GPIO_Encoder_Left_PIN_AL_PIN)
    {
        if (!DL_GPIO_readPins(GPIO_Encoder_Left_PORT,GPIO_Encoder_Left_PIN_BL_PIN)) 
        {
            Encoder_Count_Left++;
            Encoder_Count_Left_temp++;
        }
        else 
        {
            Encoder_Count_Left--;
            Encoder_Count_Left_temp--;
        }
    }
    else if((Exit_Sta_Left & GPIO_Encoder_Left_PIN_BL_PIN) == GPIO_Encoder_Left_PIN_BL_PIN)
    {
        if (!DL_GPIO_readPins(GPIO_Encoder_Left_PORT,GPIO_Encoder_Left_PIN_AL_PIN)) 
        {
            Encoder_Count_Left--;
            Encoder_Count_Left_temp--;
        }
        else 
        {
            Encoder_Count_Left++;
            Encoder_Count_Left_temp++;
        }
    }
    else if((Exit_Sta_Right & GPIO_Encoder_Right_PIN_AR_PIN) == GPIO_Encoder_Right_PIN_AR_PIN)
    {
        if(!DL_GPIO_readPins(GPIO_Encoder_Right_PORT,GPIO_Encoder_Right_PIN_BR_PIN))
        {
            Encoder_Count_Right++;
            Encoder_Count_Right_temp++;
        }
        else
        {
            Encoder_Count_Right--;
            Encoder_Count_Right_temp--;
        }
    }
    else if((Exit_Sta_Right & GPIO_Encoder_Right_PIN_BR_PIN) == GPIO_Encoder_Right_PIN_BR_PIN)
    {
        if(!DL_GPIO_readPins(GPIO_Encoder_Right_PORT,GPIO_Encoder_Right_PIN_AR_PIN))
        {
            Encoder_Count_Right--;
            Encoder_Count_Right_temp--;
        }
        else
        {
            Encoder_Count_Right++;
            Encoder_Count_Right_temp++;
        }
    }
    DL_GPIO_clearInterruptStatus(GPIO_Encoder_Left_PORT,GPIO_Encoder_Left_PIN_AL_PIN | GPIO_Encoder_Left_PIN_BL_PIN);
    DL_GPIO_clearInterruptStatus(GPIO_Encoder_Right_PORT,GPIO_Encoder_Right_PIN_AR_PIN | GPIO_Encoder_Right_PIN_BR_PIN);
}

//------------------------------------读取灰度电平状态  BEGIN--------------------------------------------------//
uint32_t Read_Huidu()
{
    uint32_t Hui[12];
    Hui[0]=DL_GPIO_readPins(HUIDU_PIN_0_PORT,HUIDU_PIN_0_PIN)==0?0:1;
    Hui[1]=DL_GPIO_readPins(HUIDU_PIN_1_PORT,HUIDU_PIN_1_PIN)==0?0:1;
    Hui[2]=DL_GPIO_readPins(HUIDU_PIN_2_PORT,HUIDU_PIN_2_PIN)==0?0:1;
    Hui[3]=DL_GPIO_readPins(HUIDU_PIN_3_PORT,HUIDU_PIN_3_PIN)==0?0:1;
    Hui[4]=DL_GPIO_readPins(HUIDU_PIN_4_PORT,HUIDU_PIN_4_PIN)==0?0:1;
    Hui[5]=DL_GPIO_readPins(HUIDU_PIN_5_PORT,HUIDU_PIN_5_PIN)==0?0:1;
    Hui[6]=DL_GPIO_readPins(HUIDU_PIN_6_PORT,HUIDU_PIN_6_PIN)==0?0:1;
    Hui[7]=DL_GPIO_readPins(HUIDU_PIN_7_PORT,HUIDU_PIN_7_PIN)==0?0:1;
    Hui[8]=DL_GPIO_readPins(HUIDU_PIN_8_PORT,HUIDU_PIN_8_PIN)==0?0:1;
    Hui[9]=DL_GPIO_readPins(HUIDU_PIN_9_PORT,HUIDU_PIN_9_PIN)==0?0:1;
    Hui[10]=DL_GPIO_readPins(HUIDU_PIN_10_PORT,HUIDU_PIN_10_PIN)==0?0:1;
    Hui[11]=DL_GPIO_readPins(HUIDU_PIN_11_PORT,HUIDU_PIN_11_PIN)==0?0:1;
    return  ((Hui[0]  << 11) |
            (Hui[1]  << 10) |
            (Hui[2]  << 9)  |
            (Hui[3]  << 8)  |
            (Hui[4]  << 7)  |
            (Hui[5]  << 6)  |
            (Hui[6]  << 5)  |
            (Hui[7]  << 4)  |
            (Hui[8]  << 3)  |
            (Hui[9]  << 2)  |
            (Hui[10] << 1)  |
            (Hui[11] << 0))&0x0FFF;
}
//--------------------------------------算出灰度误差值  BEGIN--------------------------------------//
int32_t Gray_Error;            //灰度差值第四问
uint32_t Error_less_cnt;
uint8_t flag_stop=0;
void Gray_Get_Error()
{
    switch (Read_Huidu()) 
    {
    case 0b100000000000:
        Gray_Error = 40;
        flag_stop=1;
        break;
    case 0b010000000000:
        Gray_Error = 35;
        flag_stop=1;
        break;
    case 0b001000000000:
        Gray_Error = 18;
        break;
    case 0b000100000000:
        Gray_Error = 13;
        break;
    case 0b000010000000:
        Gray_Error = 8;
        break;
    case 0b000001000000:
        Gray_Error = 3;
        break;
    case 0b000000100000:
        Gray_Error = -3;
        break;
    case 0b000000010000:
        Gray_Error = -8;
        break;
    case 0b000000001000:
        Gray_Error = -13;
        break;
    case 0b000000000100:
        Gray_Error = -18;
        break;
    case 0b000000000010:
        Gray_Error = -35;
        flag_stop=1;
        break;
    case 0b000000000001:
        Gray_Error = -40;
        flag_stop=1;
        break;
//----------------------------------------------------------------------------
    case 0b110000000000:
        Gray_Error = 38;
        flag_stop=1;
        break;
    case 0b011000000000:
        Gray_Error = 33;
        flag_stop=1;
        break;
    case 0b001100000000:
        Gray_Error = 18;
        break;
    case 0b000110000000:
        Gray_Error = 11;
        break;
    case 0b000011000000:
        Gray_Error = 6;
        break;
    case 0b000001100000:
        Gray_Error = 0;
        break;
    case 0b000000110000:
        Gray_Error = -6;
        break;
    case 0b000000011000:
        Gray_Error = -11;
        break;
    case 0b000000001100:
        Gray_Error = -18;
        break;
    case 0b000000000110:
        Gray_Error = -33;
        flag_stop=1;
        break;
    case 0b000000000011:
        Gray_Error = -38;
        flag_stop=1;
        break;
//-----------------------------------------------------------
    case 0b111000000000:
        Gray_Error = 39;
        break;
    case 0b011100000000:
        Gray_Error = 35;
        break;
    case 0b001110000000:
        Gray_Error = 29;
        break;
    case 0b000111000000:
        Gray_Error = 23;
        break;
    case 0b000011100000:
        Gray_Error = 17;
        break;
    case 0b000001110000:
        Gray_Error = -17;
        break;
    case 0b000000111000:
        Gray_Error = -23;
        break;
    case 0b000000011100:
        Gray_Error = -29;
        break;
    case 0b000000001110:
        Gray_Error = -35;
        break;
    case 0b000000000111:
        Gray_Error = -39;
        break;

    default:
        break;
    }
}





